package cc.rengu.igas.bsps.core.service.trans;


import cc.rengu.igas.bsps.common.dao.TransInfoQueryMapper;
import cc.rengu.igas.bsps.common.dao.impl.TransInfoQueryMapperImpl;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.MchntOrderInfoBean;
import cc.rengu.igas.bsps.facade.request.TransInfoQueryRequest;
import cc.rengu.igas.bsps.facade.response.TransInfoQueryResponse;
import cc.rengu.igas.share.common.util.SensitiveDataUtil;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.SrcTxnAuthCfgMapper;
import cc.rengu.oltp.service.common.dao.UserOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.SrcTxnAuthCfgMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.UserOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.common.entity.SrcTxnAuthCfg;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.List;

/**
 * 交易信息查询
 * Created by yuhongwen
 */
public class TransInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        TransInfoQueryRequest transInfoQueryRequest = new TransInfoQueryRequest();
        ConvertUtil.convertOutput(transInfoQueryRequest);
        return transInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        TransInfoQueryRequest transInfoQueryRequest = (TransInfoQueryRequest) request;
        TransInfoQueryResponse transInfoQueryResponse = new TransInfoQueryResponse();
        BeanUtil.beanCopy(transInfoQueryRequest, transInfoQueryResponse);

        TransInfoQueryMapper transInfoQueryMapper = new TransInfoQueryMapperImpl();
        List<PayOrderInfo> payOrderInfoList = null;
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        //todo userId用户标识 待确认
        String instId = transInfoQueryRequest.getHeader().getInstId();
        String mchntNo = transInfoQueryRequest.getMchntNo();
        String userId = transInfoQueryRequest.getUserId();
        String mchntOrderId = transInfoQueryRequest.getMchntOrderId();
        String origTraceNo = transInfoQueryRequest.getOrigTraceNo();
        String origSysSeqNo = transInfoQueryRequest.getOrigSysSeqNo();
        String devRiskInfo = transInfoQueryRequest.getDevRiskInfo();

        if ((null != userId && !userId.isEmpty()) && (null != mchntNo && !mchntNo.isEmpty())) {
            mchntOrderInfo = transInfoQueryMapper.selectMchntOrder(instId, mchntNo, mchntOrderId, origTraceNo, origSysSeqNo);
            payOrderInfoList = transInfoQueryMapper.selectPayOrder(instId, mchntOrderId, origTraceNo, origSysSeqNo);
        }
        MchntOrderInfoBean mchntOrderInfoBean = new MchntOrderInfoBean();

        if (null != mchntOrderInfo) {
            mchntOrderInfoBean.setMchntNo(mchntOrderInfo.getMchntNo());
            mchntOrderInfoBean.setMchntName(mchntOrderInfo.getMchntName());
            mchntOrderInfoBean.setUserId(mchntOrderInfo.getUserId());
            mchntOrderInfoBean.setUserName(mchntOrderInfo.getRemark3());
            mchntOrderInfoBean.setTransAmt(mchntOrderInfo.getOrderAmt());
            mchntOrderInfoBean.setRefundAmt(mchntOrderInfo.getRefundAmt());
            mchntOrderInfoBean.setTxnDate(mchntOrderInfo.getTxnDate());
            mchntOrderInfoBean.setTxnTime(mchntOrderInfo.getTxnTime());
            mchntOrderInfoBean.setTxnNum(mchntOrderInfo.getTxnNum());
            SrcTxnAuthCfgMapper srcTxnAuthCfgMapper = new SrcTxnAuthCfgMapperImpl();
            SrcTxnAuthCfg srcTxnAuthCfg = srcTxnAuthCfgMapper.selectSrcTxnAuthCfgByPrimaryKey(mchntOrderInfo.getInstId(), mchntOrderInfo.getSysSeqNum().substring(0, 3), transInfoQueryRequest.getHeader().getChanlId(), mchntOrderInfo.getTxnNum(), mchntOrderInfo.getBizType());
            if (srcTxnAuthCfg != null) {
                mchntOrderInfoBean.setTxnName(srcTxnAuthCfg.getTxnName());
            }
            mchntOrderInfoBean.setMchntOrderId(mchntOrderInfo.getMchntOrderId());
            mchntOrderInfoBean.setTransStatus(mchntOrderInfo.getTransStatus());
            mchntOrderInfoBean.setBizStatus(mchntOrderInfo.getBizStatus());
            //查询订单描述
            UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
            UserOrderInfo userOrderInfo = userOrderInfoMapper.selectUserOrderInfoByPrimaryKey(mchntOrderInfo.getInstId(), mchntOrderInfo.getSysSeqNum());
            mchntOrderInfoBean.setOrderDesc(userOrderInfo.getOrderDesc());
            String payTypeb = "";
            if (null != payOrderInfoList) {
                for (int i = 0; i < payOrderInfoList.size(); i++) {

                    PayOrderInfo payOrderInfo = payOrderInfoList.get(i);
                    if ("".equals(payTypeb)) {
                        if (payTypeb.indexOf(payOrderInfoList.get(i).getPayType()) == -1) {
                            payTypeb += payOrderInfoList.get(i).getPayType();
                        }

                    } else {
                        if (payTypeb.indexOf(payOrderInfoList.get(i).getPayType()) == -1) {
                            payTypeb += "|".concat(payOrderInfoList.get(i).getPayType());
                        }
                    }
                    String orderPayType = payOrderInfo.getPayType();
                    String payerAcctInfo = payOrderInfo.getPayerAcctInfo();
                    if ("00".equals(orderPayType) || "01".equals(orderPayType) || "04".equals(orderPayType)) {
                        if (!StringUtil.isEmptyOrNull(payerAcctInfo)) {
                            mchntOrderInfoBean.setPayerAcctInfo(SensitiveDataUtil.desensitization("2", payerAcctInfo).split("\\|")[0]);
                        }

                    } else {
                        mchntOrderInfoBean.setPayerAcctInfo(payOrderInfo.getPayerAcctInfo());
                    }
                    mchntOrderInfoBean.setSysSeqNum(payOrderInfo.getSysSeqNum());
                }

                mchntOrderInfoBean.setPayType(payTypeb);

            } else {
                rglog.error("未查询到交易支付订单信息!");
            }
        } else {
            rglog.error("未查询到交易商户订单信息!");
        }
        transInfoQueryResponse.setMchntOrderInfoBean(mchntOrderInfoBean);
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(transInfoQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        TransInfoQueryResponse transInfoQueryResponse = (TransInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(transInfoQueryResponse);
    }

}

